<?php
/**
 * @Auth: wonli <wonli@live.com>
 * Article.php
 */
class Article extends Base
{
    /**
     * article列表
     *
     * @cp_params p
     */
    function index( )
    {
        $page = array(
            'p' =>  isset($this->params['p'])?intval($this->params['p']):0,
            'limit' => 20,
            'half' => 3,
            'link' => array("article:page"),
        );

        $result['data'] = $this->loadModule('Article')->getArticle( $page );
        $result["tag"] = $this->loadModule("Blog")->get_all_tag();
        $result['page'] = $page;

        $this->display($result);
    }

    /**
     * article详细信息 自己控制缓存
     *
     * @cp_params id
     * @cp_cache false(type=1, expire_time=864000)
     */
    function detail( )
    {
        $id = isset($this->params['id']) ? intval($this->params['id']) : 0;

        if ($id)
        {
            $ARTICLE = new ArticleModule();
            $post_time = $ARTICLE->getPostTime($id);

            /**
             * 超过30天则写入文件缓存
             */
            if (time() > $post_time + 86400*30)
            {
                $fc = new FileCache(array(
                    'key'           =>  $this->getControllerName().DS.$this->getAction().DS.$id,
                    'cache_path'    =>  Loader::getFilePath("::cache/html/web"),
                    'expire_time'   =>  8640000,
                ));

                $content = $fc->get();
                if (! $content)
                {
                    $data = $ARTICLE->getDetail($id);

                    if ($data)
                    {
                        ob_start();
                        $this->display(array($data));
                        $content = ob_get_clean();

                        $fc->set(null, $content);

                    } else {
                        $this->to();
                    }
                }

                echo $content;
            }
            else
            {
                $data = $ARTICLE->getDetail($id);
                if ($data)
                {
                    $this->display(array($data));
                } else {
                    $this->to();
                }
            }

        } else {
            $this->to();
        }
    }
}
